<template>
  <div>
    <!-- 业务参数维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjbusinessParameters"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
      :onSyncTableData="onSyncTableData"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="saveFun" :disabled="isSave">保存</multiuse-button>
      </template>
    </FilterTable>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getList, postSave, getDealerUser } from '@/api/omSale/vehicleParameters/businessParameters';
export default {
  name: 'businessParameters',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        xhrAbort: true
      },
      selectedList: [],
      isSave: true,
      list: []
    };
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getUser();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '参数代码',
          fieldName: 'paraCode',
          placeholder: '请输入...'
        },
        {
          type: 'INPUT',
          label: '参数描述',
          fieldName: 'paraName',
          placeholder: '请输入...'
        },
        {
          type: 'RANGE_DATE',
          label: '创建日期',
          fieldName: 'beginCreatedAt|endCreatedAt',
          placeholder: '请选择',
          itemList: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'typeCode',
          placeholder: '请选择',
          itemList: this.createDictList('6000')
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '业务类型',
          dataIndex: 'typeCode',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6000'),
          render: p => {
            return <span>{this.createDictText(p.row.typeCode, '6000')}</span>;
          }
        },
        {
          title: '参数代码',
          dataIndex: 'paraCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '参数描述',
          dataIndex: 'paraName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '参数值',
          dataIndex: 'paraValue',
          sorter: true,
          filter: true,
          filterType: 'input',
          editable: true,
          defaultEditable: true,
          editType: 'text',
          editRequired: true
        },
        {
          title: '修改时间',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '修改人',
          dataIndex: 'updatedBy',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editType: 'select',
          editItems: []
        },
        {
          title: '可用标识',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          editable: true,
          defaultEditable: true,
          editType: 'select',
          editRequired: true,
          editItems: this.createDictList('1003')
        },
        {
          title: '系统数据',
          dataIndex: 'isSystemData',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '是', value: true },
            { text: '否', value: false }
          ],
          render: p => {
            return <span>{p.row.isSystemData ? '是' : '否'}</span>;
          }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'updatedBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'updatedBy').filterItems = empList;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let arr = [];
        row.map(x => {
          let json = {
            paraId: x.paraId,
            paraValue: x.paraValue,
            status: x.status
          };
          arr.push(json);
        });
        this.selectedList = arr;
        this.isSave = false;
      } else {
        this.selectedList = [];
        this.isSave = true;
      }
    },
    async saveFun() {
      let arr = [];
      this.selectedList.map(x => {
        this.list.map(y => {
          if (x.paraId == y.paraId) {
            arr.push(y);
          }
        });
      });

      let array = [];
      let is = false;
      arr.map(x => {
        if (!x.paraValue || !x.status) {
          is = 1;
        } else if (x.paraValue.length > 5) {
          is = 2;
        } else {
          delete x.$index;
          delete x.paraValuelsEdit;
          delete x.statusIsEdit;
          delete x._uid;
        }
      });
      console.log(arr, 'arr');
      if (is == 1) {
        this.$notify.error({
          title: '错误',
          message: '请维护表单内容后进行保存'
        });
        return;
      } else if (is == 2) {
        this.$notify.error({
          title: '错误',
          message: '参数长度多于6位，请重新输入'
        });
        return;
      }
      await this.$confirm('是否保存?', '业务参数新增', {
        confirmButtonText: '确定',
        cancelButtonText: '关闭',
        type: 'warning'
      }).then(async () => {
        let res = await postSave({ tmBusinessParaPOS: arr });
        if (res.success === true) {
          console.log(res.data);
          this.$notify.success({
            title: '提示信息',
            message: '保存成功'
          });
          this.fetchParams = Object.assign({}, this.fetchParams);
        }
      });
    },
    changeHandle(params) {
      this.isSave = true;
      this.fetchParams.xhrAbort = false;

      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    onSyncTableData(list) {
      this.list = list;
    }
  }
};
</script>
